package org.osomit.sacct.entity;

import org.junit.Test;
import static org.junit.Assert.*;
import org.osomit.sacct.builder.ResponseBuilder;
import org.osomit.sacct.security.utils.SecurityUtil;
import org.osomit.sacct.security.utils.SharedSecurityGenerator;
import org.stringtree.json.JSONReader;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 28, 2009
 */
public class Response_UT {

    @Test
    public void testToJSON(){
        Response resp1 = new Response();
        resp1.setRnd(SecurityUtil.base64Encode(SharedSecurityGenerator.generateSharedSecret(64)));
        resp1.setCommand("hello");
        resp1.setSuccessful(true);
        resp1.setErrorCode(null);
        List list = new ArrayList();
        list.add("Hello world");
        resp1.setReturnValue(list);

        String str = resp1.toJSON().toString();
        System.out.println("Response: " + str);

        ApplicationAccount acct1 = new ApplicationAccount();
        acct1.setName("good");
        acct1.setPassword("P@ssw0rd");
        ApplicationAccount acct2 = new ApplicationAccount();
        acct2.setName("bad");
        acct2.setPassword("P@ssw0rd");
        List value = new ArrayList();
        value.add(acct1);
        value.add(acct2);

        resp1.setReturnValue(value);
        str = resp1.toJSON().toString();
        System.out.println("Response: " + str);
    }

    @Test
    public void testBuildResponse(){
        Response resp1 = new Response();
        resp1.setRnd(SecurityUtil.base64Encode(SharedSecurityGenerator.generateSharedSecret(64)));
        resp1.setCommand("fetchAllAccounts");
        resp1.setSuccessful(true);
        resp1.setErrorCode(null);
        ApplicationAccount acct1 = new ApplicationAccount();
        acct1.setName("good");
        acct1.setPassword("P@ssw0rd");
        ApplicationAccount acct2 = new ApplicationAccount();
        acct2.setName("bad");
        acct2.setPassword("P@ssw0rd");
        List value = new ArrayList();
        value.add(acct1);
        value.add(acct2);

        resp1.setReturnValue(value);
        String str = resp1.toJSON().toString();
        System.out.println("Response: " + str);
        ResponseBuilder builder = new ResponseBuilder();
        JSONReader jsreader = new JSONReader();
        Object in = jsreader.read(str);
        Response resp = (Response) builder.build(in);
        assertEquals("fetchAllAccounts", resp.getCommand());
        assertTrue(resp.isSuccessful());
        assertNull(resp.getErrorCode());
        List list = resp.getReturnValue();
        assertNotNull(list);
        assertEquals(2, list.size());
        ApplicationAccount ac1 = (ApplicationAccount) list.get(0);
        assertEquals("good", ac1.getName());
        assertEquals("P@ssw0rd", ac1.getPassword());
        ApplicationAccount ac2 = (ApplicationAccount) list.get(1);
        assertEquals("bad", ac2.getName());
        assertEquals("P@ssw0rd", ac2.getPassword());        
    }

}
